package com.pes.arcww.controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.pes.arcww.form.Topics;
import com.pes.arcww.helper.Response_OUT;
import com.pes.arcww.service.TopicsService;
import com.pes.arcww.service.UsersService;

@Controller
@RequestMapping("/topics")
public class TopicsController {
	private static final Logger logger = LoggerFactory
			.getLogger(TopicsController.class);

	private static final String SRC_PATH_TOPICS = "/topics/";
	
	@Autowired(required = true)
	private TopicsService topicService;
	@Autowired(required = true)
	private UsersService userService;
	
	private static UtilsApp utils = new UtilsApp();
	
	@RequestMapping(value = {"/"}, method = RequestMethod.GET)
	public @ResponseBody
	ModelAndView home(@ModelAttribute("model") ModelMap model,
			BindingResult result, HttpServletRequest request) {
		
		logger.debug("topics( model, result, request)");
		ModelAndView mav = new ModelAndView();
		
		try{
			model = utils.init(request, model, userService);
			if(request.getSession().getAttribute("logged_user") == null){
				mav = new ModelAndView("redirect:/");
				return mav;
			}else{
				
				model.addAttribute("m_topicsList", topicService.listTopics("", ""));
				
				mav = new ModelAndView(SRC_PATH_TOPICS + "home");
				mav.addObject("m_site", model);
			}
		}catch(Exception e){
			logger.debug("topics( model, result, request) error: " + e.getMessage());
			System.out.println(e.getMessage());
		}
		return mav;
	}
	
	/**
	 * Add
	 */
	@RequestMapping(value = {"/add"}, method = RequestMethod.GET)
	public @ResponseBody
	ModelAndView add(@ModelAttribute("model") ModelMap model,
			BindingResult result, HttpServletRequest request) {
		
		logger.debug("add( model, result, request)");
		ModelAndView mav = new ModelAndView();
		
		try{
			model = utils.init(request, model, userService);
			if(request.getSession().getAttribute("logged_user") == null){
				mav = new ModelAndView("redirect:/");
				return mav;
			}else{
				mav = new ModelAndView(SRC_PATH_TOPICS + "add");
				mav.addObject("m_site", model);
			}
		}catch(Exception e){
			logger.debug("add( model, result, request) -" + e.getMessage());
		}
		return mav;
	}

	@RequestMapping(value = {"/add_input"} , method = RequestMethod.POST)
	public @ResponseBody
	Response_OUT add_input(@ModelAttribute("model") ModelMap model,
			BindingResult result, 
			HttpServletRequest request, 
			@RequestBody Map<String, Object> in) {
		
		Response_OUT out = new Response_OUT();
		logger.debug("add_input( model, result, request)");
			
		try{
			Topics topic = new Topics();
			topic.setTitle(in.get("title").toString());
			topic.setDescription(in.get("description").toString());
			topicService.addTopic(topic);
			
			out = new Response_OUT("success", "000");
		}catch(Exception e){
			logger.debug("add_input( model, result, request) -" + e.getMessage());
			out = new Response_OUT("error", "999", e.getMessage());		
		}
		return out;
	}
	
	
	/**
	 * Update
	 */
	@RequestMapping(value = {"/update/{id}"}, method=RequestMethod.GET)
	public @ResponseBody
	ModelAndView update(@ModelAttribute("model") ModelMap model,
			BindingResult result, HttpServletRequest request, 
			@PathVariable Integer id) {
		
		logger.debug("update( model, result, request)");
		ModelAndView mav = new ModelAndView();
		
		try{
			model = utils.init(request, model, userService);
			if(request.getSession().getAttribute("logged_user") == null){
				mav = new ModelAndView("redirect:/");
				return mav;
			}else{
				
				model.addAttribute("m_topic",(Topics) topicService.getTopic(id));
				mav = new ModelAndView(SRC_PATH_TOPICS + "update");
				mav.addObject("m_site", model);
			}
		}catch(Exception e){
			logger.debug("update( model, result, request) -" + e.getMessage());
		}
		return mav;
	}
	
	@RequestMapping(value = {"/update_input"} , method = RequestMethod.POST)
	public @ResponseBody
	Response_OUT update_input(@ModelAttribute("model") ModelMap model,
			BindingResult result, 
			HttpServletRequest request, 
			@RequestBody Map<String, Object> in) {
		
		logger.debug("update_input( model, result, request)");
		Response_OUT out = new Response_OUT();
		try{
			Topics topic = topicService.getTopic(Integer.parseInt(in.get("id").toString()));
			topic.setTitle(in.get("title").toString());
			topic.setDescription(in.get("description").toString());
			topicService.updateTopic(topic);
			out = new Response_OUT("success", "000");
			
		}catch(Exception e){
			logger.debug("update_input( model, result, request) -" + e.getMessage());	
			out = new Response_OUT("error", "999", e.getMessage());
		}
		return out;
	}

	
	@RequestMapping(value = {"/delete"} , method = RequestMethod.POST)
	public @ResponseBody
	Response_OUT delete(@ModelAttribute("model") ModelMap model,
			BindingResult result, 
			HttpServletRequest request, 
			@RequestBody Map<String, Object> in) {
		
		logger.debug("delete( model, result, request)");
		Response_OUT out = new Response_OUT();
		try{

			topicService.removeTopic(Integer.parseInt(in.get("id").toString()));
			out = new Response_OUT("success", "000");
			
		}catch(Exception e){
			logger.debug("delete( model, result, request) -" + e.getMessage());	
			out = new Response_OUT("error", "999", e.getMessage());
		}
		return out;
	}
	

	
	@RequestMapping(value = {"/loadmore"} , method = RequestMethod.POST)
	public @ResponseBody
	ModelAndView loadmore(@ModelAttribute("model") ModelMap model,
			BindingResult result, 
			HttpServletResponse response, 
			HttpServletRequest request, 
			@RequestBody Map<String, Object> in) {
		
		
		logger.debug("loadmore( model, result, request)");
		response.setContentType("application/json");
		
		ModelAndView mav = new ModelAndView();
			
		String filter = in.get("filter").toString();
		String value = in.get("content").toString();

		model = utils.init(request, model, userService);
		
		System.out.println("TYPE::" + value);
		
		if(value.equals("null")){
			model.addAttribute("m_topicsList", topicService.listTopics("", ""));
		}else{
			model.addAttribute("m_topicsList", topicService.listTopics(filter, value));
		}

		model.addAttribute("in_filter", filter);
		model.addAttribute("in_content", value);
		model.addAttribute("in_count", Integer.parseInt(in.get("count").toString()));
		model.addAttribute("in_index", Integer.parseInt(in.get("index").toString()));
		
		mav = new ModelAndView(SRC_PATH_TOPICS + "list");
		mav.addObject("m_site", model);

		return mav;
	}
	
	
	
}
